<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_predikat extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id_predikat';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_pt = isset($_POST['kd_pt']) ? strval($_POST['kd_pt']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*, a.Nama AS nama_predikat, b.Nama AS nama_universitas');
			$this->db->from('ref_predikat a');
			$this->db->join('ref_universitas b', 'b.kd_pt = a.kd_pt');
			
			if($kd_pt != ''){
				$this->db->where('a.kd_pt', $kd_pt);
			}
			
			$this->db->where('a.status', "A");
			//$this->db->where('a.kd_pt', $this->mdl_prosedur->getKodePT());
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('ref_predikat');
		$this->db->where('id_predikat', $kode);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		//if ($kode!=null)
		$this->db->where('id_predikat',$kode);
		
		$this->db->select('*');
		$this->db->from('ref_predikat');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('id_predikat', $data['id_predikat']);
		$this->db->set('kd_pt', $data['kd_pt']);
		$this->db->set('Nama', $data['Nama']);
		$this->db->set('IPKMin', $data['IPKMin']);
		$this->db->set('IPKMax', $data['IPKMax']);
		$this->db->set('Keterangan', $data['Keterangan']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->insert('ref_predikat');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('id_predikat', $data['id_predikat']);
		$this->db->set('kd_pt', $data['kd_pt']);
		$this->db->set('Nama', $data['Nama']);
		$this->db->set('IPKMin', $data['IPKMin']);
		$this->db->set('IPKMax', $data['IPKMax']);
		$this->db->set('Keterangan', $data['Keterangan']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_predikat', $data['id_predikat']);
		$result = $this->db->update('ref_predikat');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		 //query delet data  dengan ganti status db            
			$this->db->flush_cache();
			
			$this->db->set('status', "T");
		   
			$this->db->set('MUID', $this->session->userdata('user_id'));
			$this->db->set('MDATE', date('Y-m-d H:i:s'));
			
			$this->db->where('id_predikat', $kode);
			$result = $this->db->update('ref_predikat');
		   
		   
			//return
			if($result) {
					return TRUE;
			}else {
					return FALSE;
			}
	}
	
	/*function get_pdf(){
		# get data
		$this->db->flush_cache();
		$this->db->select('*', false);
		$this->db->from('ref_predikat');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Predikat', 'width'=>80, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'IPK Minimal', 'width'=>50, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'IPK Maximal', 'width'=>50, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Keterangan', 'width'=>75, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->Nama,
				$row->IPKMin,
				$row->IPKMax,
				$row->Keterangan				
			));
			$i++;
		}
		
		return $pdfdata;
		
	}*/
	
	function get_pdf(){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->select('*', false);
		$this->db->from('ref_predikat');			
		$this->db->where('ref_predikat.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
}

?>